package com.onluy.frameworkcloud.data.entity;


import com.alibaba.fastjson.JSON;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import com.onluy.frameworkcloud.data.annotation.Table;
import com.onluy.frameworkcloud.data.exception.EntityClassIsNullException;
import com.onluy.frameworkcloud.data.exception.NoColumnException;

public class Conditions extends AbstractEntity
{
    private static final long serialVersionUID = 1L;
    private Class<? extends BaseEntity> entityClazz;
    private Map<String, Field> fields;

    public Conditions(Class<? extends BaseEntity> entityClazz)
    {
        if (entityClazz == null) {
            throw new EntityClassIsNullException();
        }
        this.entityClazz = entityClazz;
        this.fields = EntityHelper.getAllColumnFields(entityClazz);
    }

    public Condition addCondition(String column)
    {
        Field field = (Field)this.fields.get(column);
        if (field == null) {
            throw new NoColumnException(((Table)this.entityClazz.getAnnotation(Table.class)).name(), column);
        }
        OperColumn operColumn = addOperColumn(column, field.getName(), null, field.getType());
        operColumn.asCondition();
        return operColumn.getCondition();
    }

    public Conditions addOrderByAsc(String column)
    {
        Field field = (Field)this.fields.get(column);
        OperColumn operColumn = addOperColumn(column, field.getName(), null, field.getType());
        operColumn.asOrderByAsc();
        return this;
    }

    public Conditions addOrderByDesc(String column)
    {
        Field field = (Field)this.fields.get(column);
        OperColumn operColumn = addOperColumn(column, field.getName(), null, field.getType());
        operColumn.asOrderByDesc();
        return this;
    }

    Class<? extends BaseEntity> getEntityClazz()
    {
        return this.entityClazz;
    }

    public String toString()
    {
        Map kv = new HashMap();
        for (String column : operColumns().keySet()) {
            kv.put(column, ((OperColumn)operColumns().get(column)).getCondition().getValues());
        }
        return JSON.toJSONString(kv, serializerFeatures);
    }
}